package com.haizhi.crm.api.controller.browsecount;


import com.haizhi.crm.biz.service.BrowseCountService;
import com.haizhi.crm.common.constant.BizStatus;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * Desc:浏览统计
 * Created by yangyijun on 2018/06/13.
 */
@Api(description = "[业务-浏览排行]" )
@RestController
@RequestMapping("/browseCount" )
public class BrowseCountController {

    private static final GLog LOG = LogFactory.getLogger(BrowseCountController.class);

    @Autowired
    private BrowseCountService browseCountService;

    @ApiOperation(value = "浏览统计", notes = "保存到mysql的browse_count表" )
    @RequestMapping(value = "/count", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Response count(@RequestParam @NotNull String entityType, @RequestParam @NotNull String entityKey) {
        try {
            browseCountService.count(entityType, entityKey);
            return Response.success();
        } catch (Exception e) {
            LOG.error(BizStatus.BROWSE_COUNT_ERROR.getDesc(), e);
            return Response.error().setErrorInfo(BizStatus.BROWSE_COUNT_ERROR);
        }
    }

    @ApiOperation(value = "查询浏览数", notes = "查询mysql的browse_count表" )
    @RequestMapping(value = "/queryBrowseCount", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Response queryBrowseCount(@RequestParam @NotNull String entityType, @RequestParam @NotNull String entityKey) {
        try {
            int count = browseCountService.queryBrowseCount(entityType, entityKey);
            return Response.success(count);
        } catch (Exception e) {
            LOG.error(BizStatus.BROWSE_COUNT_ERROR.getDesc(), e);
            return Response.error().setErrorInfo(BizStatus.BROWSE_COUNT_ERROR);
        }
    }

    @ApiOperation(value = "公用-热搜排行", notes = "limit 为topn。 entityType，可选值：Company（客户）；tv_outer_event（行外事件）；tv_inner_event（行内动态）；tv_product（产品）" )
    @GetMapping(value = "/hotSearchWords" )
    public Response hotSearchWords(String entityType, @RequestParam Integer limit) {
        return browseCountService.hotSearchWords(entityType, limit);
    }

}
